package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.CheckItemDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.CheckItem;
import com.ithema.service.CheckItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Currency;
import java.util.List;
@Service(interfaceClass = CheckItemService.class)
@Transactional
public class CheckItemServiceImpl implements CheckItemService {
 @Autowired
   private CheckItemDao checkItemDao;
    @Override
    public void add(CheckItem checkItem) {
    checkItemDao.add(checkItem);
    }

 /**
  * 检查项分页查询
  * @param queryPageBean
  * @return
  */
    @Override
 public PageResult pageQuery(QueryPageBean queryPageBean) {
     Integer currentPage = queryPageBean.getCurrentPage(); //当前页码
     Integer pageSize = queryPageBean.getPageSize(); //当前条数
     String queryString = queryPageBean.getQueryString(); //查询条件

   //完成分页查询,基于mybatis框架提供 的分页助手插件完成
     PageHelper.startPage(currentPage,pageSize);
     Page<CheckItem> page = checkItemDao.selectByCondition(queryString);
     long total = page.getTotal();
     List<CheckItem> rows = page.getResult();
     return new PageResult(total,rows);
 }

    /**
     * 根据id删除检查项
     * @param id
     */
    @Override
    public void deleteById(Integer id) {
        //判断当前检查项是否已经关联检查组
        long count = checkItemDao.findCountByCheckItemId(id);
        if (count > 0) {
            //检查项已经关联到检查组,不允许删除
        throw new RuntimeException("当前检查项被引用,不能删除"); //直接抛出异常

        }else {

            checkItemDao.deleteById(id);
        }
    }

    @Override
    public void edit(CheckItem checkItem) {
      checkItemDao.edit(checkItem);

    }

    @Override
    public CheckItem findById(Integer id) {
        return checkItemDao.findById(id);
    }

    @Override
    public List<CheckItem> findAll() {


        return  checkItemDao.findAll();
    }

}
